package com.example.company

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.company.adapter.CompanyAdapter
import com.example.company.databinding.ActivityCompanyBinding
import com.example.company.vm.CompanyViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/company/CompanyActivity")
class CompanyActivity : BaseMvvmActivity<ActivityCompanyBinding>() {

    private val companyViewModel:CompanyViewModel by viewModels()

    override fun initView() {
        companyViewModel.getCompany()
        mDataBinding.gotoMerchant.setOnClickListener {
            ARouter.getInstance().build("/company/MerchantActivity").navigation()
        }
    }

    override fun observeDataChange() {
        companyViewModel.mCompanySuccess.observe(this){item->
            ToastUtils.showLong("列表显示成功")
            mDataBinding.rv.run {
                layoutManager = LinearLayoutManager(this@CompanyActivity)
                adapter = CompanyAdapter(item).apply {
                    notifyDataSetChanged()
                    setItemOnClickListener(object :CompanyAdapter.ItemOnClickListener{
                        override fun onClick(view: View, position: Int) {
                            ARouter.getInstance().build("/company/CompanyDetailActivity").withInt("id",item[position].id).navigation()
                        }
                    })
                }
            }
        }
        companyViewModel.mError.observe(this){
            ToastUtils.showLong("列表显示失败")
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_company
}